<%--
    Document   : update
    Created on : Jul 1, 2012, 12:30:17 AM
    Author     : aspr0
--%>



<%@page import="is.model.dao.RegionDao"%>
<%@page import="is.model.dao.EmployeeDao"%>
<%@page import="is.model.dao.DepartmentDao"%>
<%@page import="is.interfaces.Dao"%>
<%@page import="is.model.exceptions.CyclingHierarchyException"%>
<%@page import="is.web.Hierarchy"%>
<%@page import="is.model.exceptions.EmptyValueException"%>
<%@page import="is.NullUtil"%>
<%@page import="is.model.exceptions.ObjectNotFoundException"%>
<%@page import="is.web.Message"%>
<%@page import="java.lang.NumberFormatException"%>
<%@page import="is.web.GetParam"%>
<%@page import="is.web.Form"%>
<%@page import="is.web.SelectOptions"%>
<%@page import="java.util.List"%>
<%@page import="is.model.bean.Region"%>
<%@page import="is.model.bean.Employee"%>
<%@page import="java.sql.SQLException"%>
<%@page import="is.model.bean.Department"%>
<%@page import="java.util.LinkedList"%>
<%@page import="java.util.Iterator"%>
<%@page import="is.model.DataSource"%>
<%@page import="is.model.bean.Department"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%
    request.setCharacterEncoding(is.Conf.ENCODING); // !!!

    Integer id = null;
    String datatype = "";
    String beanName = "";
    String content = "";

    try
    {
        id = Integer.parseInt(request.getParameter("id"));
    }
    catch ( NumberFormatException e )
    {
        /* return to index on wrong id */
        response.sendRedirect(".");
        return;
    }

    try
    {
        DataSource.establishConnection();

        Department initialDepartmentBean = new Department();
        Employee initialEmployeeBean = new Employee();
        Region initialRegionBean = new Region();

        Dao<Department> departmentDao = new DepartmentDao();
        Dao<Employee> employeeDao = new EmployeeDao();
        Dao<Region> regionDao = new RegionDao();

        departmentDao.setInitialBean(initialDepartmentBean);
        employeeDao.setInitialBean(initialEmployeeBean);
        regionDao.setInitialBean(initialRegionBean);

        Department department = null;
        Department parentDepartment = null;
        Employee boss = null;
        Region region = null;

        List<Department> departments = new LinkedList<Department>();
        List<Employee> employees = new LinkedList<Employee>();
        List<Region> regions = new LinkedList<Region>();

        /* Retrieving department and linked objects */
        initialDepartmentBean.setId(id);
        departmentDao.objectExists();
        department = departmentDao.retrieve();

        Integer parentId = department.getParentId();
        if ( parentId != null )
        {
            initialDepartmentBean.setId(parentId);
            parentDepartment = departmentDao.retrieve();
        }

        Integer bossId = department.getBossId();
        if ( bossId != null )
        {
            initialEmployeeBean.setId(bossId);
            boss = employeeDao.retrieve();
        }

        initialRegionBean.setId(department.getRegionId());
        region = regionDao.retrieve();

        /* Retrieving lists for building option selects */
        Integer departmentsCount = departmentDao.count();
        List<Department> allDepartments = departmentDao.retrieveMultiple(0, departmentsCount, -1);
        /* Filtering child departments from parent candidats */
        Hierarchy<Department> hierarchy = new Hierarchy<Department>();
        hierarchy.setBeans(allDepartments);
        hierarchy.build();
        departments = hierarchy.excludeChild(department);


        /* Selecting employees from current department - boss candidats */
        initialEmployeeBean = new Employee();
        initialEmployeeBean.setDepartmentId(department.getId());
        employeeDao.setInitialBean(initialEmployeeBean);
        employees = employeeDao.search(true);

        Integer regionsCount = regionDao.count();
        regions = regionDao.retrieveMultiple(0, regionsCount, -1);


        LinkedList<String> selects = new LinkedList<String>();

        selects.add(new SelectOptions<Department>().generate(departments, department.getParentId(), true, "parentId", 0));
        selects.add(new SelectOptions<Employee>().generate(employees, department.getBossId(), true, "bossId", 0, 1, 2));
        selects.add(new SelectOptions<Region>().generate(regions, department.getRegionId(), false, "regionId", 0));

        String form = Form.generate(department, selects, 1, 2, 3);

        datatype = department.getTableName();
        beanName = department.getName();
        content = form;

        if (request.getMethod().equals("POST"))
        {
%>

<jsp:useBean id="departmentNew" class="is.model.bean.Department" scope="request"/>
<jsp:setProperty name="departmentNew" property="*"/>

<%
            NullUtil.prove(departmentNew.getName());
            NullUtil.prove(departmentNew.getRegionId());
            hierarchy.proveParent(departmentNew);

            departmentDao.setInitialBean(departmentNew);
            departmentDao.update();
            response.sendRedirect("?"+GetParam.INFO+"=0");
        }

        DataSource.closeConnection();
    }
    catch(EmptyValueException e)
    {
        content = Message.genMsgWarning("1") + content;
    }
    catch(CyclingHierarchyException e)
    {
        response.sendRedirect(".");
        return;
    }
    catch (SQLException e)
    {
        e.printStackTrace();
        response.sendRedirect("?"+GetParam.ERROR+"=0");
        return;
    }
    catch(ObjectNotFoundException e)
    {
        response.sendRedirect(".");
        return;
    }
    catch (Exception e)
    {
        if(e.getCause().getCause().getClass() == new NumberFormatException().getClass())
        {
            content = Message.genMsgWarning("0") + content;
        }
        else
        {
            e.printStackTrace();
            response.sendRedirect("?"+GetParam.ERROR+"=1");
            return;
        }
    }
%>

<jsp:include page="../template/update.jsp">
    <jsp:param name="datatype" value="<%=datatype%>"/>
    <jsp:param name="beanName" value="<%=beanName%>"/>
    <jsp:param name="content" value="<%=content%>"/>
</jsp:include>